package collect.taobao.util.page;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Ormaping {
	public Class EntityClass;
	public String TableName;
	public String Sequance;
	public String PriKeyName;
	
	public Ormaping(Class EntityClass,String TableName,String Sequance,String PriKeyName) {
		this.EntityClass=EntityClass;
		this.TableName=TableName;
		this.Sequance=Sequance;
		this.PriKeyName=PriKeyName;
	}
	
	public String getSelectSQL() {
		String sql="";
		
		sql+="select "+getFieldListStr()+" from "+TableName+" where 1=1 ";
		
		return sql;
	}
	
	public String getFieldListStr() {
		String fl="";
		Field [] fs=EntityClass.getFields();
		int count=0;
		for (int i=0;i<fs.length;i++) {
			if (!Modifier.isStatic(fs[i].getModifiers())) {
				if (count>0) fl+=",";
				fl+=fs[i].getName();
				count++;
			}
		}
		return fl;
	}
	
	public String getInserParamListStr() {
		String fl="";
		Field [] fs=EntityClass.getFields();
		int count=0;
		for (int i=0;i<fs.length;i++) {
			if (!Modifier.isStatic(fs[i].getModifiers())) {
				if (count>0) fl+=",";
				count++;
					fl+="?";
			}
		}
		return fl;
	}	
	
	public String getUpdateFieldListStr() {
		String fl="";
		Field [] fs=EntityClass.getFields();
		int count=0;
		for (int i=0;i<fs.length;i++) {
			if (!Modifier.isStatic(fs[i].getModifiers())
					&&!fs[i].getName().equals(PriKeyName)) {
				if (count>0) fl+=",";
				count++;
				fl+=fs[i].getName()+"=?";
			}
		}
		return fl;
	}
	
	public String getUpdateFieldListStrNotNull(Object v) throws IllegalArgumentException, IllegalAccessException {
		String fl="";
		Field [] fs=EntityClass.getFields();
		int count=0;
		for (int i=0;i<fs.length;i++) {
			if (!Modifier.isStatic(fs[i].getModifiers())
					&&!fs[i].getName().equals(PriKeyName)
					&&fs[i].get(v)!=null) {
				if (count>0) fl+=",";
				count++;
				fl+=fs[i].getName()+"=?";
			}
		}
		return fl;
	}
	
}

